import { NavigationGuardNext, RouteLocationNormalized } from "vue-router";
import { message } from "ant-design-vue";

export function requireAuth(
  to: RouteLocationNormalized,
  from: RouteLocationNormalized,
  next: NavigationGuardNext
) {
  const token = localStorage.getItem("token");
  const isAuthenticated = !!token;

  if (to.meta.requiresAuth && !isAuthenticated) {
    console.warn("需要登录");
    message.error("请先登录");
    return next({ path: "/hot" });
  }

  next();
}
